package taxicall.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.taxicall.entities.Taxi;
import com.taxicall.mocks.TaxicallMocks;


public class TaxiRepository {
	
	private List<Taxi> taxis;
	
	public TaxiRepository() {
		taxis = new ArrayList<Taxi>();
		// get some sample taxis to start with
		taxis.addAll(createSampleTaxis());
	}

	// update taxi if it exists or add it
	public void addOrUpdate(Taxi taxi) {
		for(Taxi t : taxis){
			if(taxi.getId() == t.getId()){
				// taxi exists, update it
				t.setLocation(taxi.getLocation());
				t.setAvailableSeats(taxi.getAvailableSeats());
				
				return;
			}
		}

		// taxi does not exist, add it
		taxis.add(taxi);
	}
	
	public List<Taxi> getAll(){
		return taxis;
	}
	
	private List<Taxi> createSampleTaxis() {
		try {
			return new TaxicallMocks().getTaxis();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

}